package com.xyz.frame.api.controller;

import com.xyz.frame.api.dto.R;
import com.xyz.frame.api.service.OssService;
import com.xyz.utils.ConstantPropertiesUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/eduoss/fileoss")
public class SzyOssController {


    @Autowired
    private OssService ossService;


    //上传头像的方法
    @Schema(description = "上传头像")
    @PostMapping
    public com.xyz.entity.R uploadOssFile(@Schema(name = "file", description = "文件", required = true)
                                          @RequestParam("file") MultipartFile file,
                                          @Schema(name = "host",description = "文件上传路径", required = false)
                                          @RequestParam(value = "host", required = false) String host) {
        if(!StringUtils.isEmpty(host)){
            ConstantPropertiesUtils.FILE_HOST = host;
        }
//        String url = ossService.uploadFileAvatar(file);
        String url = ossService.uploadFileAvatarLocal(file);
        return com.xyz.entity.R.ok().data("url", url);
    }
    @Schema(description = "上传课件附件")
    @PostMapping("/course")
    public com.xyz.entity.R uploadCourseFile(@Schema(name = "file", description = "文件", required = true)
                                             @RequestParam("file") MultipartFile file,
                                             @Schema(name = "host",description = "文件上传路径", required = false)
                                             @RequestParam(value = "host", required = false) String host) {
        if(!StringUtils.isEmpty(host)){
            ConstantPropertiesUtils.FILE_HOST = host;
        }
//        String url = ossService.uploadFileAvatar(file);
        String url = ossService.uploadCourseFile(file);
        return com.xyz.entity.R.ok().data("url", url);
    }

    //直接根据接口地址找到本地图片返回
    //接口地址格式为：http://127.0.0.1:8222/eduoss/fileoss/getLocalImage/Users/gds/project2025/tmp/eduUpload/images/2025/09/02/1756814763375.jpg
    @Schema(description = "根据接口地址获取本地图片")
    @GetMapping("/getLocalImage/**")
    public void getLocalImage(HttpServletRequest request,
                              HttpServletResponse response) throws IOException {
        // 从URL中提取本地文件路径
        // 获取请求URI
        String requestURI = request.getRequestURI();
        // 移除基础路径，获取实际的文件路径
        String localPath = requestURI.split("getLocalImage")[1];

        ossService.getLocalImage(localPath,response);
    }
    //    @Schema(description = "根据接口地址获取本地图片")
//    @GetMapping("/getLocalVideo/**")
//    public void getLocalVideo(HttpServletRequest request,
//                              HttpServletResponse response) throws IOException {
//        // 从URL中提取本地文件路径
//        // 获取请求URI
//        String requestURI = request.getRequestURI();
//        // 移除基础路径，获取实际的文件路径
//        String localPath = requestURI.split("getLocalVideo")[1];
//
//        ossService.getLocalVideo(localPath,response);
//    }
    @Schema(description = "根据接口地址获取本地图片")
    @GetMapping("/getLocalVideo/**")
    public void getLocalVideo(HttpServletRequest request,
                              HttpServletResponse response) throws IOException {
        // 从URL中提取本地文件路径
        // 获取请求URI
        String requestURI = request.getRequestURI();
        // 移除基础路径，获取实际的文件路径
        String localPath = requestURI.split("getLocalVideo")[1];

        ossService.getLocalVideo(localPath,response);
    }

    @Schema(description = "根据接口地址获取本地文件")
    @GetMapping("/getLocalFile/**")
    public void getLocalFile(HttpServletRequest request,
                             HttpServletResponse response) throws IOException {
        // 从URL中提取本地文件路径
        // 获取请求URI
        String requestURI = request.getRequestURI();
        // 移除基础路径，获取实际的文件路径
        String localPath = requestURI.split("getLocalFile")[1];

        ossService.getLocalFile(localPath,response);
    }

}